package com.dtp.extension.skywalking.wrapper;

import com.dtp.core.support.runnable.MdcRunnable;
import com.dtp.core.support.wrapper.TaskWrapper;
import org.apache.skywalking.apm.toolkit.trace.RunnableWrapper;
import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import org.slf4j.MDC;

import static com.dtp.common.constant.DynamicTpConst.TRACE_ID;

/**
 * Skywalking跟踪任务包装器
 *
 * @author yanhom
 * @since 1.0.8
 *
 * @remark muse
 **/
public class SwTraceTaskWrapper implements TaskWrapper {

    private static final String NAME = "swTrace"; // 任务包装器名称

    @Override
    public String name() {
        return NAME;
    }

    @Override
    public Runnable wrap(Runnable runnable) {
        MDC.put(TRACE_ID, TraceContext.traceId()); // 调用Skywalking方法获得traceId，并赋值给MDC
        return MdcRunnable.get(new RunnableWrapper(runnable)); // 运行任务
    }
}
